package org.jeecg.modules.performance.controller;

import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.performance.entity.PerformanceMonth;
import org.jeecg.modules.performance.entity.PerformanceMonthDepart;
import org.jeecg.modules.performance.service.ICalculatePerforService;
import org.jeecg.modules.performance.service.IPerformanceMonthDepartService;
import org.jeecg.modules.performance.service.IPerformanceMonthService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.performance.service.impl.CalculatePerforServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: performance
 * @Author: jeecg-boot
 * @Date:   2021-05-18
 * @Version: V1.0
 */
@Slf4j
@Api(tags="performance")
@RestController
@RequestMapping("/performance/performanceMonth")
public class PerformanceMonthController extends JeecgController<PerformanceMonth, IPerformanceMonthService> {
	@Autowired
	private IPerformanceMonthService performanceMonthService;
	@Autowired
	private IPerformanceMonthDepartService performanceMonthDepartService;

	@Autowired
	private ICalculatePerforService calculatePerforService;
	/**
	 * 分页列表查询
	 *
	 * @param performanceMonth
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "performance-分页列表查询")
	@ApiOperation(value="performance-分页列表查询", notes="performance-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PerformanceMonth performanceMonth,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PerformanceMonth> queryWrapper = QueryGenerator.initQueryWrapper(performanceMonth, req.getParameterMap());
		Page<PerformanceMonth> page = new Page<PerformanceMonth>(pageNo, pageSize);
		IPage<PerformanceMonth> pageList = performanceMonthService.page(page, queryWrapper);
		for (PerformanceMonth record : pageList.getRecords()) {
			QueryWrapper<PerformanceMonthDepart> queryWrapperDe = new QueryWrapper<>();
			queryWrapperDe.eq("perfor_month_id",record.getId());
			List<PerformanceMonthDepart> list = performanceMonthDepartService.list(queryWrapperDe);
		    if(CollectionUtils.isEmpty(list)){
				record.setFeedback("0/0");
				record.setSureStatus(0);
			}else {
		    	Integer sure = 0;
				for (PerformanceMonthDepart performanceMonthDepart : list) {
					if(performanceMonthDepart.getSureFlag().equals(2)){
						sure ++;
					}
				}
				record.setFeedback(sure+"/"+ list.size());


			}

		}
		return Result.ok(pageList);
	}

	 /**
	  * 添加
	  *
	  * @param performanceMonth
	  * @return
	  */
	 @AutoLog(value = "performance-添加")
	 @ApiOperation(value="performance-添加", notes="performance-添加")
	 @PostMapping(value = "/add")
	 public Result<?> add(@RequestBody PerformanceMonth performanceMonth) {
		 performanceMonthService.save(performanceMonth);
		 return Result.ok("添加成功！");
	 }



	 /**
	  * 添加
	  *
	  * @param performanceMonth
	  * @return
	  */
	 @AutoLog(value = "performance-拆分绩效")
	 @ApiOperation(value="performance-拆分绩效", notes="performance-添加")
	 @PostMapping(value = "/split")
	 public Result<?>  split(@RequestBody PerformanceMonth performanceMonth) {
		 calculatePerforService.chaifenJixiao( performanceMonth);
		 Map<String,Object>  resultMap = new HashMap<>();
		 resultMap.put("id",performanceMonth.getId());
		 return Result.ok(resultMap);
	 }


	 /**
	 * 编辑
	 *
	 * @param performanceMonth
	 * @return
	 */
	@AutoLog(value = "performance-编辑")
	@ApiOperation(value="performance-编辑", notes="performance-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PerformanceMonth performanceMonth) {
		performanceMonthService.updateById(performanceMonth);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "performance-通过id删除")
	@ApiOperation(value="performance-通过id删除", notes="performance-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		performanceMonthService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "performance-批量删除")
	@ApiOperation(value="performance-批量删除", notes="performance-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.performanceMonthService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "performance-通过id查询")
	@ApiOperation(value="performance-通过id查询", notes="performance-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		PerformanceMonth performanceMonth = performanceMonthService.getById(id);
		return Result.ok(performanceMonth);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param performanceMonth
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, PerformanceMonth performanceMonth) {
      return super.exportXls(request, performanceMonth, PerformanceMonth.class, "performance");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, PerformanceMonth.class);
  }

}
